package com.inforich.app.api.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.app.api.dto.ProjectDTO;
import com.inforich.app.api.service.IAppProjectService;
import com.inforich.app.component.LoginUserApp;
import com.inforich.appManage.domain.Contact;
import com.inforich.appManage.service.IContactService;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.service.IProjectService;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/28
 */
@Service
public class AppProjectServiceImpl implements IAppProjectService {

  @Autowired
  private IProjectService projectService;
  @Autowired
  private IContactService contactService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult addProject(LoginUserApp loginUserApp, ProjectDTO projectDTO) {
    Project project = getMyProject(loginUserApp);
    if (project != null) {
      return AjaxResult.error("项目信息已存在，不可重复添加");
    }
    project = new Project();
    // 判断项目名称是否有重复
    int count = projectService.count(new LambdaQueryWrapper<Project>()
        .eq(Project::getName, projectDTO.getName()));
    if (count > 0) {
      return AjaxResult.error("项目名称已被认证");
    }
    BeanUtils.copyProperties(projectDTO, project);
    projectService.insertProject(project);
    // 添加项目负责人账户关系
    contactService.addProject(loginUserApp.getId(), project.getId());
    return AjaxResult.success();
  }

  @Override
  public Project getMyProject(LoginUserApp loginUserApp) {
    String projectId = contactService.getProjectIdByAppUserId(loginUserApp.getId());
    if (StrUtil.isBlank(projectId)) {
      return null;
    }
    return projectService.getProjectById(projectId);
  }

  @Override
  public void createDoc(LoginUserApp loginUserApp, HttpServletResponse response)
      throws UnsupportedEncodingException {
    ExcelWriter writer = ExcelUtil.getWriter();
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    String fileName = "项目信息.xlsx";
    fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    ServletOutputStream out = null;
    try {
      out = response.getOutputStream();
      writer.flush(out, true);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      writer.close();
      if (out != null) {
        IoUtil.close(out);
      }
    }
  }

}
